<?php
	
function list_biaya_golongan_jabatan(){

//PASANG JAVASCRIPT
my_set_code_js('

function confirmDelete(id){
	var t = confirm(\'Yakin akan menghapus data ?\');
	if(t){
		location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
	}
	return false;
}

');
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Golongan'=>array('style'=>'border-bottom:2px solid;width:15%'), 
		'Komponen Biaya'=>array('style'=>'border-bottom:2px solid;40%'), 
		'Nominal (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;15%'), 
		'Maksimal (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;15%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "
		SELECT * 
		FROM spd_biaya_golongan_jabatan a
		INNER JOIN spd_biaya b ON b.biaya_id = a.biaya_id
		INNER JOIN golongan_jabatan c ON c.golongan_jabatan_id = a.golongan_jabatan_id ";
	if( isset($_GET['key']) ){
		$key = trim($_GET['key']);
		if($key<>'')
		$query .= "WHERE b.label_biaya LIKE '%{$key}%' ";		
	}
	if( isset($_GET['sortBy']) ){
		$query .= " ORDER BY  a.`{$_GET['sortBy']}` ASC ";
	}
	
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['biaya_golongan_jabatan_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['biaya_golongan_jabatan_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$nominal=$maksimal=0;
		if($ey['tipe']=="Nominal"){
			$nominal=$ey['nominal'];
		}else{
			$maksimal=$ey['nominal'];
		}
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'golongan'=>  $ey['kode_golongan_jabatan'] ,   
			'divisi'=>  $ey['label_biaya'] ,   
			'nominal'=>  position_text_align( rp_format($nominal) ,   'right'),
			'maksimal'=>  position_text_align( rp_format($maksimal) ,   'right'),
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);
	$menuontop = array();
	$scr = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'"/>
	<table width="100%">
	<tr>
		<td width="210"> &nbsp; Search <input type="text" name="key" value="'.$_GET['key'].'"/> </td>
		<td width="170">By &nbsp;<select name="sortBy" >
			<option '.( $_GET['sortBy']=="golongan_jabatan_id" ? "selected " : " ").'value="golongan_jabatan_id">Golongan Jabatan</option>
			<option '.( $_GET['sortBy']=="biaya_id" ? "selected " : " ").'value="biaya_id">Nama Biaya</option> 
		</select></td>
		<td><input type="submit" value=" GO "/> </td> 
	</tr>
	</table>
	</form>';
	
	$printmenu = additional_menu_on_list( $menuontop , $scr);
	return $scr .table_builder($header , $datas , 5 , false  );
}


function form_biaya_validate($id){
	$errsubmit = false;
	$err = array();
	
	$gol =  trim($_POST["golongan_jabatan_id"]); 
	if( $gol == '0'){
		$errsubmit = true;
		$err[] = "Golongan jabatan belum diisi";
	}
	$by =  trim($_POST["biaya_id"]); 
	if( $by == '0'){
		$errsubmit = true;
		$err[] = "Komponen biaya belum diisi";
	}	
	
	$query="SELECT biaya_golongan_jabatan_id FROM spd_biaya_golongan_jabatan 
		WHERE golongan_jabatan_id=".$gol.
		" AND biaya_id=".$by. 
		" AND biaya_golongan_jabatan_id<>".$id; 

	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Golongan dan komponen biaya sudah ada";	
	}		

	if(! is_numeric($_POST["nominal"] )  ){
		$errsubmit = true;
		$err[] = "Nominal belum benar";	
	}	
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function form_biaya_submit($id){
	$datas = array(
		'golongan_jabatan_id'=>my_type_data_int( $_POST['golongan_jabatan_id'] ),
		'biaya_id'=>my_type_data_int( $_POST['biaya_id'] ), 
		'nominal'=>my_type_data_str( set_text_as_number($_POST['nominal'] )), 
	);
	if($id > 0){
		return my_update_record('spd_biaya_golongan_jabatan' ,'biaya_golongan_jabatan_id' , $id , $datas);
	}
	
	return my_insert_record('spd_biaya_golongan_jabatan' , $datas);
}

function form_biaya_create($id = 0){
	$view = form_header( "devisi" , "devisi"  );
 	$fields = my_get_data_by_id('spd_biaya_golongan_jabatan','biaya_golongan_jabatan_id',$id);
	
	$atemps = array();
	$query = "SELECT * FROM golongan_jabatan";
	$result = my_query($query);
	while($row = my_fetch_array($result) ){
		$atemps[$row['golongan_jabatan_id']] = $row['kode_golongan_jabatan'];
	}
	$temp = array(
		'name'=>'golongan_jabatan_id',
		'value'=>( isset($_POST['golongan_jabatan_id']) ? $_POST['golongan_jabatan_id'] : $fields['golongan_jabatan_id'] ),
		'id'=>'golongan_jabatan_id', 
	);
	$form_temp = form_dropdown($temp ,$atemps); 
	$view .= form_field_display( $form_temp   , "Golongan Jabatan"   );
		
	$atemps = array();
	$query = "SELECT * FROM spd_biaya";
	$result = my_query($query);
	while($row = my_fetch_array($result) ){
		$atemps[$row['biaya_id']] = $row['label_biaya'];
	}
	$temp = array(
		'name'=>'biaya_id',
		'value'=>( isset($_POST['biaya_id']) ? $_POST['biaya_id'] : $fields['biaya_id'] ),
		'id'=>'biaya_id', 
	);
	$form_temp = form_dropdown($temp ,$atemps); 
	$view .= form_field_display( $form_temp   , "Komponen Biaya"   );
	
	$temp = array(
		'name'=>'nominal',
		'value'=>( isset($_POST['nominal']) ? $_POST['nominal'] : rp_format($fields['nominal'] )),
		'id'=>'nominal',
		'type'=>'text',
		'size'=>'20'
	);
	$form_temp = form_money($temp);	
	$view .= form_field_display( 'Rp. '.$form_temp   , "Nominal/Maksimal"   );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}
function remove_biaya($id){
	$query= "DELETE FROM spd_biaya_golongan_jabatan WHERE biaya_golongan_jabatan_id = {$id}";
	return my_query($query);
} 
 